package com.luxuewen.sxa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luxuewen.sxa.common.annotation.ApiAuth;
import com.luxuewen.sxa.entity.SxaDictionaries;
import com.luxuewen.sxa.entity.SxaDictionaryDetails;
import com.luxuewen.sxa.service.impl.SxaDictionariesServiceImpl;
import com.luxuewen.sxa.service.impl.SxaDictionaryDetailsServiceImpl;
import com.luxuewen.sxa.utils.response.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wen
 */
@RestController
@Slf4j
@Validated
public class DictionaryController {
    @Autowired
    private SxaDictionariesServiceImpl sxaDictionariesService;

    @Autowired
    private SxaDictionaryDetailsServiceImpl sxaDictionaryDetailsService;

    @ApiAuth(alias = "dic:list")
    @GetMapping("dic/list")
    public ApiResponse<List<Map<String, Object>>> list () {
        QueryWrapper<SxaDictionaries> sxaDictionariesQueryWrapper = new QueryWrapper<>();
        sxaDictionariesQueryWrapper.select("id", "name", "code", "description", "status");

        return ApiResponse.success(sxaDictionariesService.listMaps(sxaDictionariesQueryWrapper));
    }

    @ApiAuth(alias = "dic:create")
    @PostMapping("dic/create")
    public ApiResponse<Integer> create (@Validated @RequestBody SxaDictionaries sxaDictionaries) {
        QueryWrapper<SxaDictionaries> sxaDictionariesQueryWrapper = new QueryWrapper<>();
        sxaDictionariesQueryWrapper.eq("code", sxaDictionaries.getCode());

        boolean ex = sxaDictionariesService.exists(sxaDictionariesQueryWrapper);
        if (ex) {
            return ApiResponse.fail("字典分类编码已存在，请勿重复创建！");
        }

        boolean res = sxaDictionariesService.save(sxaDictionaries);
        if (!res) {
            return ApiResponse.fail("字典分类创建失败，请稍后再试！");
        }

        return ApiResponse.success(sxaDictionaries.getId());
    }

    @ApiAuth(alias = "dic:create")
    @PostMapping("dic/update")
    public ApiResponse<Integer> update (@Validated @RequestBody SxaDictionaries sxaDictionaries) {
        QueryWrapper<SxaDictionaries> sxaDictionariesQueryWrapper = new QueryWrapper<>();
        sxaDictionariesQueryWrapper.eq("code", sxaDictionaries.getCode())
                .ne("id", sxaDictionaries.getId());

        boolean ex = sxaDictionariesService.exists(sxaDictionariesQueryWrapper);
        if (ex) {
            return ApiResponse.fail("字典分类编码已存在，请勿重复创建！");
        }

        boolean res = sxaDictionariesService.updateById(sxaDictionaries);
        if (!res) {
            return ApiResponse.fail("字典分类更新失败，请稍后再试！");
        }

        return ApiResponse.success(sxaDictionaries.getId());
    }

    @ApiAuth(alias = "dic:item:list")
    @GetMapping("dic/item/list/{dicId}/{page}/{limit}")
    public ApiResponse<Page<Map<String, Object>>> itemList (@PathVariable Integer dicId, @PathVariable Integer page, @PathVariable Integer limit) {
        QueryWrapper<SxaDictionaryDetails> sxaDictionaryDetailsQueryWrapper = new QueryWrapper<>();
        sxaDictionaryDetailsQueryWrapper.eq("dic_id", dicId);

        Page<Map<String, Object>> ePage = new Page<>(page, limit);

        return ApiResponse.success(sxaDictionaryDetailsService.pageMaps(ePage, sxaDictionaryDetailsQueryWrapper));
    }

    @ApiAuth(alias = "dic:item:create")
    @PostMapping("dic/item/create")
    public ApiResponse<Integer> itemCreate (@Validated @RequestBody SxaDictionaryDetails sxaDictionaryDetails) {
        QueryWrapper<SxaDictionaryDetails> sxaDictionaryDetailsQueryWrapper = new QueryWrapper<>();
        sxaDictionaryDetailsQueryWrapper.eq("dic_id", sxaDictionaryDetails.getDicId())
                .eq("value", sxaDictionaryDetails.getValue());

        boolean ex = sxaDictionaryDetailsService.exists(sxaDictionaryDetailsQueryWrapper);
        if (ex) {
            return ApiResponse.fail("字典项键值已存在，请勿重复创建！");
        }

        boolean res = sxaDictionaryDetailsService.save(sxaDictionaryDetails);
        if (!res) {
            return ApiResponse.fail("字典项创建失败，请稍后再试！");
        }

        return ApiResponse.success(sxaDictionaryDetails.getId());
    }

    @ApiAuth(alias = "dic:item:update")
    @PostMapping("dic/item/update")
    public ApiResponse<Integer> itemUpdate (@Validated @RequestBody SxaDictionaryDetails sxaDictionaryDetails) {
        QueryWrapper<SxaDictionaryDetails> sxaDictionaryDetailsQueryWrapper = new QueryWrapper<>();
        sxaDictionaryDetailsQueryWrapper.eq("dic_id", sxaDictionaryDetails.getDicId())
                .eq("value", sxaDictionaryDetails.getValue())
                .ne("id", sxaDictionaryDetails.getId());

        boolean ex = sxaDictionaryDetailsService.exists(sxaDictionaryDetailsQueryWrapper);
        if (ex) {
            return ApiResponse.fail("字典项键值已存在，请勿重复创建！");
        }

        boolean res = sxaDictionaryDetailsService.updateById(sxaDictionaryDetails);
        if (!res) {
            return ApiResponse.fail("字典项更新失败，请稍后再试！");
        }

        return ApiResponse.success(sxaDictionaryDetails.getId());
    }

    @ApiAuth(alias = "dic:item:delete")
    @PostMapping("dic/item/delete/{id}")
    public ApiResponse<Integer> itemDelete (@PathVariable Integer id) {
        boolean res = sxaDictionaryDetailsService.removeById(id);
        if (!res) {
            return ApiResponse.fail("字典项删除失败，请稍后再试！");
        }

        return ApiResponse.success();
    }

    @ApiAuth(alias = "dic:delete")
    @PostMapping("dic/delete/{id}")
    public ApiResponse<Integer> delete (@PathVariable Integer id) {
        boolean res = sxaDictionariesService.removeById(id);
        if (!res) {
            return ApiResponse.fail("字典删除失败，请稍后再试！");
        }

        return ApiResponse.success();
    }
}
